package com.hula.onion.book.server.spider.platforms.honglingchuangtou;

import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.MD5Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/30 19:49
 *
 * @author liangbing
 * @version 1.0
 */
@Component
public class HongLingChuangTouSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(4);

        map.put("phone", form.getUserName());
        map.put("encryption", MD5Utils.MD5Encode(form.getPassword()));
        map.put("loginCategory", 6);
        map.put("app_key", "16ae450f970448619c3ce7193982089e");
        map.put("requestId", "my089-18020610-4937-8149-8328-551099841756");

        return map;
    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public AccountInfo parseAccountBalance(String html) {

        AccountInfo accountInfo = new AccountInfo();

        if (!StringUtils.isEmpty(html)) {

            Document dc = Jsoup.parse(html);

            String amount = dc.getElementById("spanTotalAmount").text().substring(1);

            accountInfo.setAmount(new BigDecimal(amount));

            String totalIntrest = dc.getElementById("spanIncomeAmount").text().substring(1);

            accountInfo.setTotalIntrest(new BigDecimal(totalIntrest));
        }


        return accountInfo;
    }


}
